<?php
	defined ('CELL_INDEX') or die ('access denied');
	isset ($_SESSION ['user']) or die ('No session data available.');
	$User = $_SESSION ['user'];

	$MyNumber = pg_fetch_all (pg_query ("select \"Number\" from \"Users\" where \"Username\" = '$User';"));
	isset ($MyNumber [0]['Number']) or die ('No number assigned.');
	$MyNumber = $MyNumber [0]['Number'];

	$NumbersWork = pg_fetch_all (pg_query ("select \"Number\" from \"NumbersWork\" where \"Owner\" = $MyNumber;"));
	$NumbersPersonal = pg_fetch_all (pg_query ("select \"Number\" from \"NumbersPersonal\" where \"Owner\" = $MyNumber;"));
	$NumbersStray = pg_fetch_all (pg_query ("select distinct \"Dest\" from \"Calls\" where \"Owner\" = $MyNumber union select distinct \"Dest\" from \"SMS\" where \"Owner\" = $MyNumber except select \"Number\" from \"NumbersWork\" where \"Owner\" = $MyNumber except select \"Number\" from \"NumbersPersonal\" where \"Owner\" = $MyNumber;"));
?>
<h1>User's pane</h1>


<?php if ($NumbersStray) { ?>
<h2>Unassigned numbers</h2>
<ul>
	<?php foreach ($NumbersStray as $Number) { ?><li><?php echo ($Number ['Dest']) ?></li><?php } ?>
</ul>
<?php } ?>

<h2>Work numbers</h2>
<?php if ($NumbersWork) { ?>
<ul>
	<?php foreach ($NumbersWork as $Number) { ?><li><?php echo ($Number ['Number']) ?></li><?php } ?>
</ul>
<?php } ?>
<form action="" method="post">
	<input type="text" name="work" />
	<button type="submit" name="action" value="user">Add</button>
</form>

<h2>Personal numbers</h2>
<?php if ($NumbersPersonal) { ?>
<ul>
	<?php foreach ($NumbersPersonal as $Number) { ?><li><?php echo ($Number ['Number']) ?></li><?php } ?>
</ul>
<?php } ?>
<form action="" method="post">
	<input type="text" name="personal" />
	<button type="submit" name="action" value="user">Add</button>
</form>
